/* eslint-disable no-param-reassign */
import modelApi from '@/api/modules/system/modelMana'
import vehicleApi from '@/api/modules/system/vehicleMana'
import lodash from 'lodash'
const initQueryParams = {
  vin: undefined,
  vehicleType: undefined,
  modelIds: undefined,
  // 车辆版本
  vehicleVersion: undefined,
  brandId: undefined,
  currentVehicleVersion: undefined
}
const initVehicleEcuUpdateQuery = {
  ecuIds: [],
  taskIds: [],
  status: '',
  vehicleVersion: []
}
const initVehicleAppUpdateQuery = {
  softwareDids: [],
  taskIds: [],
  status: ''
}
const initRightDrawerEvent = {
  // 触发的事件名
  key: null,
  // 触发事件所传递的数据
  data: null
}
const state = {
  oneBrandVehicleData: [],
  types: [{
    id: 1,
    name: 'normalVehicle'
  }, {
    id: 2,
    name: 'testVehicle'
  }, {
    id: 3,
    name: 'seedVehicle'
  }],
  vehicleData: [],
  loadingVehicleData: false,
  // 查询条件
  queryParams: lodash.cloneDeep(initQueryParams),
  // 当前车辆信息
  activeVehicleData: null,
  // 当前车辆下的零件信息
  vehiclePartData: [],
  loadingVehiclePartData: false,
  // 车辆ecu升级信息查询条件
  vehicleEcuUpdateQuery: lodash.cloneDeep(initVehicleEcuUpdateQuery),
  // 车辆详情
  vehicleDetail: {},
  // 是否正在加载车辆详情
  loadingVehicleDetail: false,
  rightDrawerEvent: { ...initRightDrawerEvent },
  // 车辆app升级信息查询条件
  vehicleAppUpdateQuery: lodash.cloneDeep(initVehicleAppUpdateQuery)
}
const getters = {
  vehicleTypeObj: state => () => {
    const obj = {}
    state.types.forEach(item => {
      obj[item.id] = item.name
    })
    return obj
  }

}
const mutations = {
  SET_ONE_BRAND_VEHICLE_DATA (state, val) {
    state.oneBrandVehicleData = val
  },
  SET_VEHICLE_DATA (state, val) {
    state.vehicleData = val
  },
  SET_LOADING_VEHICLE_DATA (state, data) {
    state.loadingVehicleData = data
  },
  SET_ACTIVE_VEHICLE_DATA (state, data) {
    state.activeVehicleData = data
  },
  // eslint-disable-next-line no-unused-vars
  SET_VEHICLE_PART_DATA (state, { vehicleId, data }) {
    state.vehiclePartData = data
  },
  SET_LOADING_VEHICLE_PART_DATA (state, data) {
    state.loadingVehiclePartData = data
  },
  SET_VEHICLE_DETAIL (state, data) {
    state.vehicleDetail = data
  },
  SET_LOADING_VEHICLE_DETAIL (state, data) {
    state.loadingVehicleDetail = data
  },
  // 清空查询条件
  resetVehicleQueryParams (state) {
    state.queryParams = lodash.cloneDeep(initQueryParams)
  },
  resetVehicleEcuUpdateQuery (state) {
    state.vehicleEcuUpdateQuery = lodash.cloneDeep(initVehicleEcuUpdateQuery)
  },
  setVehicleRightDrawerEvent (state, val) {
    state.rightDrawerEvent = val
  },
  resetVehicleRightDrawerEvent (state) {
    state.rightDrawerEvent = { ...initRightDrawerEvent }
  },
  resetVehicleAppUpdateQuery (state) {
    state.vehicleAppUpdateQuery = lodash.cloneDeep(initVehicleAppUpdateQuery)
  }
}

const actions = {
  // 获取某品牌下的车型
  getVehicleDataByBrandId ({ commit }, brandId) {
    commit('SET_ONE_BRAND_VEHICLE_DATA', [])
    return new Promise((resolve, reject) => {
      modelApi.getMyAllModel({ brandId }).then(res => {
        commit('SET_ONE_BRAND_VEHICLE_DATA', res.data)
        resolve(true)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 添加
  // eslint-disable-next-line no-unused-vars
  addVehicleData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      vehicleApi.addVehicle(data).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 编辑
  // eslint-disable-next-line no-unused-vars
  editVehicleData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      vehicleApi.editVehicle(data).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取分页车辆数据
  getVehiclePageData ({ commit }, data) {
    commit('SET_LOADING_VEHICLE_DATA', true)
    commit('SET_VEHICLE_DATA', [])
    return new Promise((resolve, reject) => {
      vehicleApi.getVehiclePageData(data).then(res => {
        commit('SET_VEHICLE_DATA', res.data)
        resolve(res)
      }).catch(err => {
        reject(err)
      }).finally(() => {
        commit('SET_LOADING_VEHICLE_DATA', false)
      })
    })
  },
  // 获取指定车辆的零件信息
  getVehicleParts ({ commit }, query) {
    commit('SET_LOADING_VEHICLE_PART_DATA', true)
    commit('SET_VEHICLE_PART_DATA', { vehicleId: query.vehicleId, data: [] })
    return new Promise((resolve, reject) => {
      vehicleApi.getVehiclePartData(query).then(res => {
        commit('SET_VEHICLE_PART_DATA', { vehicleId: query.vehicleId, data: res.data })
        resolve(res)
      }).catch(err => {
        reject(err)
      }).finally(() => {
        commit('SET_LOADING_VEHICLE_PART_DATA', false)
      })
    })
  },
  // 获取车辆按车型分组统计数据
  // eslint-disable-next-line no-unused-vars
  getVehicleModelReportData ({ commit }, query) {
    return new Promise((resolve, reject) => {
      vehicleApi.getVehicleModelReport(query).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取车辆按品牌分组统计数据
  // eslint-disable-next-line no-unused-vars
  getVehicleBrandReportData ({ commit }, query) {
    return new Promise((resolve, reject) => {
      vehicleApi.getVehicleBrandReport(query).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取某车辆升级信息时间轴数据
  // eslint-disable-next-line no-unused-vars
  getVehicleEcuTimelineData ({ commit }, query) {
    return new Promise((resolve, reject) => {
      vehicleApi.getVehicleEcuTimeline(query).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 下载导入车辆文件的模板
  // eslint-disable-next-line no-unused-vars
  downloadVehicleExcelTpl ({ commit }, query) {
    return new Promise((resolve, reject) => {
      vehicleApi.downloadVehicleExcelTpl(query).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 导入车辆
  // eslint-disable-next-line no-unused-vars
  uploadVehicleData ({ commit }, data) {
    return new Promise((resolve, reject) => {
      vehicleApi.uploadVehicleExcel(data).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取车辆详情信息
  getVehicleInfoData ({ commit }, data) {
    commit('SET_VEHICLE_DETAIL', {})
    commit('SET_LOADING_VEHICLE_DETAIL', true)
    return new Promise((resolve, reject) => {
      vehicleApi.getVehicleInfo(data).then(res => {
        commit('SET_VEHICLE_DETAIL', res.data || {})
        resolve(res)
      }).catch(err => {
        reject(err)
      }).finally(() => {
        commit('SET_LOADING_VEHICLE_DETAIL', false)
      })
    })
  },
  // 获取某车辆SOTA升级信息时间轴数据
  // eslint-disable-next-line no-unused-vars
  getVehicleAppTimelineData ({ commit }, query) {
    return new Promise((resolve, reject) => {
      vehicleApi.getVehicleAppTimeline(query).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}
export default {
  state,
  getters,
  mutations,
  actions
}
